package hu.bme.aut.consulttime.client.components;

import hu.bme.aut.consulttime.client.TeamService;
import hu.bme.aut.consulttime.client.TeamServiceAsync;
import hu.bme.aut.consulttime.entitydtos.ProjectDTO;
import hu.bme.aut.consulttime.entitydtos.TeamDTO;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

public class ModifyTeam extends DialogBox {

	private TeamServiceAsync teamService = GWT.create(TeamService.class);

	private TextBox textBoxName;
	private ListBox comboBoxSemester;
	private ListBox comboBoxProject;
	private FlexTable flexTable;
	private Button btnModify;

	public ModifyTeam(final TeamDTO teamDTO, List<ProjectDTO> projectDTOs,
			List<String> semesters) {
		super(true);

		flexTable = new FlexTable();

		Label lblNewLabel = new Label("Modify Team");
		flexTable.setWidget(0, 0, lblNewLabel);

		Label lblProjectTitle = new Label("Team Name");
		flexTable.setWidget(1, 0, lblProjectTitle);

		textBoxName = new TextBox();
		flexTable.setWidget(1, 1, textBoxName);
		textBoxName.setWidth("100%");
		// set default
		textBoxName.setText(teamDTO.getName());

		// --------------------- Upload the Projects ---------------------
		Label lblProject = new Label("Project Name (Type)");
		flexTable.setWidget(2, 0, lblProject);

		comboBoxProject = new ListBox();
		flexTable.setWidget(2, 1, comboBoxProject);
		comboBoxProject.setWidth("100%");

		for (ProjectDTO tmp : projectDTOs) {
			comboBoxProject.addItem(tmp.getTitle() + " (" + tmp.getType() + ")"
					+ " -id#" + tmp.getProjectId());
		}
		// set default
		for (int i = 0; i < comboBoxProject.getItemCount(); ++i) {
			String chosen = comboBoxProject.getItemText(i);
			String id = chosen.substring(chosen.lastIndexOf("#") + 1);
			if (Long.parseLong(id) == teamDTO.getProjectId()) {
				comboBoxProject.setSelectedIndex(i);
			}
		}

		// --------------------- Upload the Semesters ---------------------
		Label lblSemester = new Label("Project Type");
		flexTable.setWidget(3, 0, lblSemester);

		comboBoxSemester = new ListBox();
		flexTable.setWidget(3, 1, comboBoxSemester);
		comboBoxSemester.setWidth("100%");

		for (String tmp : semesters) {
			comboBoxSemester.addItem(tmp);
		}
		// set default
		String teamSemester = teamDTO.getSemesterId();
		for (int i = 0; i < comboBoxSemester.getItemCount(); ++i) {
			if (comboBoxSemester.getItemText(i).equals(teamSemester)) {
				comboBoxSemester.setSelectedIndex(i);
			}
		}


		btnModify = new Button("Modify");
		btnModify.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				TeamDTO modifiedTeamDTO = new TeamDTO();
				modifiedTeamDTO.setTeamId(teamDTO.getTeamId());
				modifiedTeamDTO.setName(textBoxName.getText());
				modifiedTeamDTO.setSemesterId((comboBoxSemester
						.getValue(comboBoxSemester.getSelectedIndex())));
				String chosen = comboBoxProject.getValue(comboBoxProject
						.getSelectedIndex());
				String id = chosen.substring(chosen.lastIndexOf("#") + 1);
				modifiedTeamDTO.setProjectId(Long.parseLong(id));


				teamService.updateTeamInfos(modifiedTeamDTO,
						new AsyncCallback<Boolean>() {

							@Override
							public void onSuccess(Boolean result) {
								if (result == true) {
									Window.confirm("'" + teamDTO.getName()
											+ "' team modified!");
							        //Refresh the table
							        TeamPage.loadTeams();
								} else {
									Window.alert("Team modification failed!");
								}
							}

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Remote Procedure Call - Failure");
							}
						});
			}
		});
		flexTable.setWidget(4, 1, btnModify);
		
		setWidget(flexTable);
	}
}
